package com.example.uibestpractice

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.EditText
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.uibestpractice.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity(), View.OnClickListener {

    private val msgList = ArrayList<Msg>()
    private lateinit var adapter: MsgAdapter
    private lateinit var binding: ActivityMainBinding
    private var talkCount: Int = 0
    private  val msgListPrepare = ArrayList<Msg>()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        initMsg()
        val layoutManager = LinearLayoutManager(this)
        binding.recyclerView.layoutManager = layoutManager
        if (!::adapter.isInitialized) {
            adapter = MsgAdapter(msgList)
        }
        binding.recyclerView.adapter = adapter
        binding.send.setOnClickListener(this)

    }

    override fun onClick(p0: View?) {

        when(p0) {

            binding.send -> {
                val content = binding.inputText.text.toString()
                if(content.isNotEmpty()) {
                    val msg = Msg(content, Msg.TYPE_SENT)
                    msgList.add(msg)
                    adapter.notifyItemInserted(msgList.size - 1)
                    binding.recyclerView.scrollToPosition(msgList.size -1)
                    binding.inputText.setText("")

                }else {

                    Log.e("empty","clicked")
                    if(talkCount <= 12)
                    {
                        msgList.add(msgListPrepare[talkCount])
                        adapter.notifyItemInserted(msgList.size - 1)
                        binding.recyclerView.scrollToPosition(msgList.size -1)
                    }
                    talkCount ++
                    Log.e("outPut","finished")
                }

            }
        }

    }

    private fun initMsg() {

        val msg1 = Msg("I am fine, and you?", Msg.TYPE_RECEIVED)
        val msg2 = Msg("It's ok.", Msg.TYPE_RECEIVED)
        val msg3 = Msg("Yeah,You're never really done as long as you have a good story and some one to tell it to.", Msg.TYPE_RECEIVED)
        val msg4 = Msg("We laughed and kept saying\"see u soon\",but inside we both knew we'd never see each other again.", Msg.TYPE_RECEIVED)
        val msg5 = Msg("I guess it comes down a simple choice. Get busy living or get busy dying.", Msg.TYPE_RECEIVED)
        val msg6 = Msg("Whoever saves one life, saves the world entire.", Msg.TYPE_RECEIVED)

        val msg11 = Msg("Hello, How are you?", Msg.TYPE_SENT)
        val msg22 = Msg("Not complained, your family?", Msg.TYPE_SENT)
        val msg33 = Msg("You make millions of decisions that mean nothing and then one day your order takes out and it changes your life.", Msg.TYPE_SENT)
        val msg44 = Msg("You know how everyone's saying seize the moment? I don't know, I'm kind of thinking, it's the other way around, you know, like the moment seizes us.", Msg.TYPE_SENT)
        val msg55 = Msg("Life was like a box of chocolates, you never know what you're gonna get.", Msg.TYPE_SENT)
        val msg66 = Msg("People should not be afraid of their governments. Governments should be afraid of their people.", Msg.TYPE_SENT)

        msgListPrepare.add(msg11)
        msgListPrepare.add(msg1)
        msgListPrepare.add(msg22)
        msgListPrepare.add(msg2)
        msgListPrepare.add(msg33)
        msgListPrepare.add(msg3)
        msgListPrepare.add(msg44)
        msgListPrepare.add(msg4)
        msgListPrepare.add(msg55)
        msgListPrepare.add(msg5)
        msgListPrepare.add(msg66)
        msgListPrepare.add(msg6)


    }


}